﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Collections.Extensions;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Akso.Authorization;
using Akso.CodeGroups.Dto;
using Akso.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;

namespace Akso.CodeGroups
{
    [AbpAuthorize(AppPermissions.Pages_BasicDataModule_CodeSettings)]
    public class CodeGroupAppService: AbpZeroTemplateAppServiceBase, ICodeGroupAppService
    {
        private readonly IRepository<CodeGroupEntity, Guid> _codegroupsRepository;
        public CodeGroupAppService(IRepository<CodeGroupEntity, Guid> codegroupsRepository)
        {
            _codegroupsRepository = codegroupsRepository;
        }
        #region 查询数据
        private IQueryable<CodeGroupEntity> getQuery(GetCodeGroupsInput input)
        {
            var query = _codegroupsRepository.GetAll()
                .WhereIf(
                    !input.Filter.IsNullOrWhiteSpace(),
                    t =>
                        t.CodeGroupName.Contains(input.Filter)
                );
            return query;
        }
        public async Task<ListResultDto<CodeGroupListDto>> GetObjects(GetCodeGroupsInput input)
        {
            var query = getQuery(input);
            var dataCount = await query.CountAsync();
            var data = await query.OrderBy(input.Sorting).PageBy(input).ToListAsync();

            return new PagedResultDto<CodeGroupListDto>(
                dataCount,
                data.MapTo<List<CodeGroupListDto>>()
                );
        }
        #endregion

        #region 新增、编辑对象，带权限检查
        /// <summary>
        /// 新增、编辑对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_BasicDataModule_CodeGroups_Create, AppPermissions.Pages_BasicDataModule_CodeGroups_Edit)]
        public async Task<CodeGroupEntity> CreateOrUpdateObject(CreateOrUpdateCodeGroupInput input)
        {
            if (input.CodeGroup.Id.HasValue)
            {
                return await UpdateObject(input);
            }
            else
            {
                return await CreateObject(input);
            }
        }

        /// <summary>
        /// 新增对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_BasicDataModule_CodeGroups_Create)]
        protected virtual async Task<CodeGroupEntity> CreateObject(CreateOrUpdateCodeGroupInput input)
        {
            var o = input.CodeGroup.MapTo<CodeGroupEntity>();
            //检查名称是否存在
            int i = await _codegroupsRepository.CountAsync(x => x.CodeGroupKey == o.CodeGroupKey);
            if (i > 0)
            {

                Debug.Fail(L("YouCanNotDeleteOwnAccount"));
            }
            return await _codegroupsRepository.InsertAsync(o);
        }
        /// <summary>
        /// 编辑对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_BasicDataModule_CodeGroups_Edit)]
        protected virtual async Task<CodeGroupEntity> UpdateObject(CreateOrUpdateCodeGroupInput input)
        {
            Debug.Assert(input.CodeGroup.Id != null, "input.Classroom.Id should be set.");

            var o = await _codegroupsRepository.GetAsync(input.CodeGroup.Id.Value);
            //Update classroom properties
            input.CodeGroup.MapTo(o);
            return await _codegroupsRepository.UpdateAsync(o);
        }
        #endregion

        #region 根据Guid获取数据对象
        /// <summary>
        /// 新增、编辑页面中获取对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_BasicDataModule_CodeGroups_Create, AppPermissions.Pages_BasicDataModule_CodeGroups_Edit)]
        public async Task<GetCodeGroupForEditOutput> GetObjectForEdit(NullableIdDto<Guid> input)
        {
            var output = new GetCodeGroupForEditOutput
            {

            };

            if (!input.Id.HasValue)
            {
                //Creating a new object
                output.CodeGroup = new CodeGroupEditDto();
            }
            else
            {
                //Editing an existing object
                var existingObject = await _codegroupsRepository.GetAsync(input.Id.Value);

                output.CodeGroup = existingObject.MapTo<CodeGroupEditDto>();
            }

            return output;
        }
        #endregion
    }
}
